package com.itboye.eweilisten.me.personal.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.adapter.BooksAdapter;
import com.itboye.eweilisten.adapter.GuessLikeAdapter;
import com.itboye.eweilisten.base.BaseActivity;
import com.itboye.eweilisten.base.BasePtr;
import com.itboye.eweilisten.me.home.ActivityExperience;
import com.itboye.eweilisten.model.GuessBooks;
import com.itboye.eweilisten.model.MyBookBean;
import com.itboye.eweilisten.presenter.UserPresenter;
import com.itboye.eweilisten.util.loadingutil.MAlert;
import com.itboye.eweilisten.util.widget.IsStringSPUtils;
import com.itboye.eweilisten.volley.ResultEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * 已购书籍(我的微测试)
 */
public class ActivityBooks extends BaseActivity implements Observer {
    private TextView tvTitle;
    private ImageView back;
    private LinearLayout ll_like;
    private BooksAdapter adapter;
    private TextView tv_word;
    private GridView guessLikeView;
    private GuessLikeAdapter guessLikeAdapter;
    GuessBooks guessBooks = new GuessBooks();
    List<GuessBooks.ListInfo> listGuessInfo;

    PtrFrameLayout commend_anchor_ptr;

    private ListView boosListView;
    MyBookBean beans;
    List<MyBookBean.ListInfo> listInfo;
    UserPresenter presenter;

    int page_index = 1;
    int page_size = 10;
    ImageView img_kong;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_presenter_books);
        tvTitle.setText("我的微测试");
        BasePtr.setPagedPtrStyle(commend_anchor_ptr);//设置上拉下拉刷新

        beans = new MyBookBean();
        listInfo = new ArrayList<>();
        presenter = new UserPresenter(this);
        // presenter.onMyBooks(IsStringSPUtils.IsUid(),"1","10");
        Log.d("===onYiMaiBook",IsStringSPUtils.IsUid()+"=="+page_index+"==="+page_size+"");
        presenter.onYiMaiBook(IsStringSPUtils.IsUid(), "", "", page_index + "", page_size + "",IsStringSPUtils.getSid());
        adapter = new BooksAdapter(this, listInfo);
        boosListView.setAdapter(adapter);

        listGuessInfo = new ArrayList<>();
        presenter.onGuessLike("3", "1", "16",IsStringSPUtils.IsUid(),IsStringSPUtils.getSid());//每页3个，第一页，16表示推荐书籍
        guessLikeView = (GridView) findViewById(R.id.gv_guesslike);
        guessLikeAdapter = new GuessLikeAdapter(this, listGuessInfo);
        guessLikeView.setAdapter(guessLikeAdapter);
//        if(listGuessInfo.size()<=0){
//            ll_like.setVisibility(View.GONE);
//        }
        commend_anchor_ptr.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                page_index++;
                presenter.onYiMaiBook(IsStringSPUtils.IsUid(), "", "", page_index + "", page_size + "",IsStringSPUtils.getSid());
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.onYiMaiBook(IsStringSPUtils.IsUid(), "", "", page_index + "", page_size + "",IsStringSPUtils.getSid());
            }
        });
        commend_anchor_ptr.autoRefresh();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.back:
                this.finish();
                break;
        }
    }

    @Override
    public void update(Observable o, Object arg) {
        ResultEntity entity = handlerError(arg);
        if (entity != null) {
            if (entity.getEventType() == presenter.yigou_book_success) {
                if (page_index == 1) {
                    listInfo.clear();
                }
                commend_anchor_ptr.refreshComplete();//设置刷新完成

                beans = (MyBookBean) entity.getData();
                listInfo.addAll(beans.getList());
                if (listInfo.size() > 0) {
                    commend_anchor_ptr.setVisibility(View.VISIBLE);
                    boosListView.setVisibility(View.VISIBLE);
                    img_kong.setVisibility(View.GONE);
                    adapter.notifyDataSetChanged();

                    boosListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent = new Intent(ActivityBooks.this, ActivityExperience.class);
                            intent.putExtra("countment", beans.getList().get(position).getSecondary_headlines());
                            intent.putExtra("name", beans.getList().get(position).getName());
                            intent.putExtra("imgid", beans.getList().get(position).getImg_main());
                            intent.putExtra("id", beans.getList().get(position).getP_id());
                            //是否过期参数：bought
                            if((Long.valueOf(beans.getList().get(position).getExpire_time())- (System.currentTimeMillis()/1000)) <0){
                                intent.putExtra("bought",false);
                                MAlert.alert("已过期请重新购买");
                            }else {
                                intent.putExtra("bought",true);
                                startActivity(intent);
                            }

                        }
                    });
                } else {
                    commend_anchor_ptr.setVisibility(View.GONE);
                    boosListView.setVisibility(View.GONE);
                    img_kong.setVisibility(View.VISIBLE);
                }
            }
            if (entity.getEventType() == presenter.guess_like_success) {
                guessBooks = (GuessBooks) entity.getData();
                listGuessInfo.addAll(guessBooks.getList());
                tv_word.setText("猜你喜欢");
                if (listGuessInfo.size() > 0) {
                    guessLikeAdapter.notifyDataSetChanged();

                    guessLikeView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent = new Intent(ActivityBooks.this, ActivityExperience.class);
                            intent.putExtra("countment", guessBooks.getList().get(position).getSecondary_headlines());
                            intent.putExtra("name", guessBooks.getList().get(position).getName());
                            intent.putExtra("imgid", guessBooks.getList().get(position).getMain_img());
                            intent.putExtra("id", guessBooks.getList().get(position).getP_id());
                            startActivity(intent);
                        }
                    });
                }
            }
            if (entity.getEventType() == presenter.guess_like_fail) {
                MAlert.alert(entity.getData());
            }
            if (entity.getEventType() == presenter.yigou_book_fail) {
                MAlert.alert(entity.getData());
            }
        }
    }
}
